﻿var status = require('http-status');
var express = require('express');
var router = express.Router();
var Question = require('../models/question');

router.post('/', function (req, res) {
    if (!req.user) {
        return res.status(status.UNAUTHORIZED).end();
    }
    if (req.user.realm != 'student') {
        return res.status(status.FORBIDDEN).json({error: 'Not Allowed'});
    }
    var _question = req.body.question || {};
    _question.creator = req.user._id;
    Question.create(_question, function (err, question) {
        if (err) {
            return res.status(status.BAD_REQUEST).end();
        } else {
            return res.status(status.CREATED).json({question: question.tidyInfo()});
        }
    });
});

router.get('/subject/:subject', function (req, res) {
    if (!req.user) {
        return res.status(status.UNAUTHORIZED).end();
    }
    Question.find({subject: req.params.subject}, 'title _id subject creator content createdAt').populate('creator', '_id username').exec(function (err, questions) {
        if (err) {
            return res.status(status.INTERNAL_SERVER_ERROR).end();
        }
        if (questions.length === 0) {
            return res.status(status.NO_CONTENT).end();
        } else {
            res.status(status.OK).json({questions: questions});
        }
    });
});

router.get('/id/:id', function (req, res) {
    if (!req.user) {
        return res.status(status.UNAUTHORIZED).end();
    }
    Question.findById(req.params.id, 'title _id subject creator content createdAt').populate('creator', '_id username').exec(function (err, question) {
        if (err) {
            return res.status(status.INTERNAL_SERVER_ERROR).end();
        }
        if (!question) {
            return res.status(status.NOT_FOUND).json({error: 'Question Not Found'});
        }
        return res.status(status.OK).json({question: question});
    });
});

router.delete('/id/:id', function (req, res) {
    if (!req.user) {
        return res.status(status.UNAUTHORIZED).end();
    }
    Question.findById(req.params.id, 'title _id subject creator content createdAt').populate('creator', '_id username').exec(function (err, question) {
        if (err) {
            return res.status(status.INTERNAL_SERVER_ERROR).end();
        }
        if (!question) {
            return res.status(status.NOT_FOUND).json({error: 'Question Not Found'});
        }
        if (question.creator._id.toString() !== req.user._id.toString()) {
            console.log(question.creator._id, req.user._id);
            return res.status(status.FORBIDDEN).end();
        } else {
            question.remove(function (err) {
                if (err) {
                    return res.status(status.INTERNAL_SERVER_ERROR).end();
                } else {
                    res.status(status.OK).end();
                }
            })
        }
    });
});


router.get('/', function (req, res) {
    if (!req.user) {
        return res.status(status.UNAUTHORIZED).end();
    }
    if (req.user.realm === 'teacher') {
        Question.find({subject: req.user.subject}, 'title _id subject creator content createdAt').populate('creator', '_id username').exec(function (err, questions) {
            if (err) {
                return res.status(status.INTERNAL_SERVER_ERROR).end();
            }
            if (questions.length === 0) {
                return res.status(status.NO_CONTENT).end();
            } else {
                return res.status(status.OK).json({questions: questions});
            }
        });
    } else {
        Question.find({creator: req.user._id}, 'title _id subject creator content createdAt').populate('creator', '_id username').exec(function (err, questions) {
            if (err) {
                return res.status(status.INTERNAL_SERVER_ERROR).end();
            }
            if (questions.length === 0) {
                return res.status(status.NO_CONTENT).end();
            } else {
                res.status(status.OK).json({questions: questions});
            }
        });
    }
});


module.exports = router;